package com.we823.project.repository.component;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.we823.project.model.repo.MaterialBomModel;
import com.we823.project.model.repo.MaterialModel;
import com.we823.project.model.repo.MaterialStockOutModel;
import com.we823.project.repository.service.repo.MaterialService;

@Component("calculateUtil")
public class CalculateUtil {
	
	private MaterialService materialService;
	
	@Autowired
	public CalculateUtil(MaterialService materialService) {
		this.materialService = materialService;
	}	

	public List<MaterialStockOutModel> getByBom(
			List<MaterialBomModel> materialBomModels, String mainCode,float needTotal) {
		
			if(materialBomModels!=null && materialBomModels.size()>0) {
				List<MaterialStockOutModel> stockOutModels = Lists.newArrayList();
				List<String> materialCodeList = Lists.newArrayList();
				
				materialCodeList.add(mainCode);
				for (MaterialBomModel materialBomModel : materialBomModels) {
					materialCodeList.add(materialBomModel.getMaterialCode());
				}
				
				//快速查找material的信息，减少与数据库的访问
				List<MaterialModel> materialModels = materialService.findByCodeIn(materialCodeList);
				if (materialModels!=null && materialModels.size()>0) {
					Map<String, MaterialModel> materialMap = Maps.newHashMap();
					for (MaterialModel materialModel : materialModels) {
						materialMap.put(materialModel.getCode(), materialModel);
					}

					for (MaterialBomModel materialBomModel : materialBomModels) {
						MaterialStockOutModel materialStockOutModel = new MaterialStockOutModel();
						
						String materialCode = materialBomModel.getMaterialCode();
						materialStockOutModel.setMaterialCode(materialCode);
						
						MaterialModel materialModel = materialMap.get(materialCode);
						
						materialStockOutModel.setMaterial(materialModel);
						
						String storeType = materialBomModel.getStoreType();
						int flag = 0; //需要仓库确认并出库
						if (!StringUtils.isEmpty(storeType) && "m".equals(storeType.toLowerCase())) {
							flag = 2; //无需仓库确认，为半成品
						}
						float needNum = 0 ;
						int cacluteCode = materialBomModel.getCaculateCode();
						String finalUnit = "";

						needNum = needTotal / cacluteCode * materialBomModel.getNeedNum();
						//保留4位小数
						needNum = (float)Math.round(needNum*10000)/10000;
						if (materialModel!=null) {
							finalUnit = materialModel.getUnit();
						}

						materialStockOutModel.setNeedNum(needNum);
						materialStockOutModel.setFinalUnit(finalUnit);
						materialStockOutModel.setFlag(flag);
						
						stockOutModels.add(materialStockOutModel);
						
					}
				}
				
				return stockOutModels;
			}
				
		return null;
	}

}
